package xhwl.hmo.mall.views;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import xhwl.hmo.mall.R;
import xhwl.hmo.mall.http.bean.resp.CommentListResp;
import xhwl.hmo.mall.ui.checkpic.ImagePagerActivity;
import xhwl.hmo.mall.utils.DimenUtil;
import xhwl.hmo.mall.utils.StringUtil;

/**
 * Created by Administrator on 2017/10/11.
 */

public class ForumPictureView extends LinearLayout {
    private Context mContext;
    private GridViewforScrollView gridView;
    private PicAdapter adapter;
    List<String> mList = new ArrayList<>();
    private ImageView imageView;
    private String id;
    private CommentListResp mCommentListResp;

    public ForumPictureView(Context context) {
        this(context, null);
    }

    public ForumPictureView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(final Context context) {
        mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.forum_picture_view, this);
        gridView = (GridViewforScrollView) findViewById(R.id.gridView);
        adapter = new PicAdapter(context,mList);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Intent intent = new Intent(context, ImageShowerActivity.class);
//                intent.putExtra("imgshow", mList.get(position));
//                intent.putExtra("type", "4");
//                context.startActivity(intent);
                imageBrower(position, mList);
            }
        });
    }
    /**
     * 打开图片查看器
     *  @param position
     * @param urls2*/
    protected void imageBrower(int position, List<String> urls2) {
        Intent intent = new Intent(mContext, ImagePagerActivity.class);
        // 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, (Serializable) urls2);
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
        mContext.startActivity(intent);
    }
    public void setData2(CommentListResp forumProductResp) {
        if (forumProductResp != null) {
            id = "1";
            mCommentListResp = forumProductResp;

        }
    }

    public void setData(String urls, int max) {
        mList.clear();
        if (!StringUtil.isEmpty(urls)) {
            String[] picUrl = urls.split(";");
            if (picUrl != null && picUrl.length != 0) {
                if (picUrl.length == 1) {
                    mList.add("placeHolder");
                }
                for (int i = 0; i < max && i < picUrl.length; i++) {
                    mList.add(picUrl[i]);
//                    measureStocksGvHeight();//测量item的高度
                }

            }
        }
        adapter.notifyDataSetChanged();
    }

    private void measureStocksGvHeight() {
        if (adapter == null || adapter.getCount()
                < 1) {
            return;
        }
        int totalHeight = 0;
        View listItem = adapter.getView(0, null, gridView);
        listItem.measure(0, 0);

        if (mList.size() > 0 && mList.size() <= 2) {
            totalHeight = listItem.getMeasuredHeight();
        } else if (mList.size() % 2 <= 0) {
            totalHeight = listItem.getMeasuredHeight() * (mList.size() / 2 + 1);
        } else if (mList.size() % 2 > 0) {
            totalHeight = listItem.getMeasuredHeight() * (mList.size() / 2 + 1);
        }
        ViewGroup.LayoutParams params = gridView.getLayoutParams();
        params.height = totalHeight;
        gridView.setLayoutParams(params);
    }

    private class PicAdapter extends BaseAdapter {
        List<String> mList;
        Context context;

        public PicAdapter(Context context,List<String> mList) {
            this.mList = mList;
            this.context=context;
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
            params.height = DimenUtil.dip2px(mContext, mContext.getResources().getDimension(R.dimen.forum_pic_height));
            imageView.setLayoutParams(params);
            if ("placeHolder".equals(mList.get(position))) {
                imageView.setVisibility(View.INVISIBLE);
            } else {
                Glide.with(context)
                        .load(mList.get(position))
                        .error(R.drawable.forum_default)
                        .into(imageView);
            }
            return imageView;
        }
    }
}
